/**
 * 依赖树视图修复工具
 * 提供一个简化的依赖树生成函数，用来解决依赖树无法显示的问题
 */
import * as vscode from 'vscode';
import { Dependency } from './dependencyAnalyzer';
 
/**
 * 生成简化版依赖树HTML
 * 不使用复杂的递归逻辑，直接生成最简单的树形结构
 */
export function generateSimpleTreeHtml(dependencies: Dependency[], pomPath: string): string {
    // 检查依赖数据
    if (!dependencies || !Array.isArray(dependencies)) {
        console.error('无效的依赖数据');
        return '<div class="error-message">无效的依赖数据</div>';
    }

    if (dependencies.length === 0) {
        return '<div class="no-dependencies">没有找到依赖</div>';
    }

    // 获取根依赖
    const rootDeps = dependencies.filter(d => !d.path || d.path.length === 0);
    console.log(`找到${rootDeps.length}个根依赖`);

    let html = `
        <div class="tree-view simple">
            <div class="summary">依赖树总览: ${dependencies.length}个依赖, ${rootDeps.length}个根依赖</div>
            <ul class="root-list">
    `;

    // 只处理根依赖，避免任何复杂的递归逻辑
    for (const dep of rootDeps) {
        const hasConflict = dep.conflicts && dep.conflicts.length > 0;
        const version = dep.version || '未指定';
        const scope = dep.scope || 'compile';

        html += `
            <li class="root-dep${hasConflict ? ' conflict' : ''}">
                <div class="dep-line">
                    <span class="dep-name">${escapeHtml(dep.groupId)}:${escapeHtml(dep.artifactId)}</span>
                    <span class="dep-version">${escapeHtml(version)}</span>
                    <span class="dep-scope">${escapeHtml(scope)}</span>
                </div>
                <div class="dep-children">
                    <div class="load-more">
                        <button class="load-more-btn" 
                            onclick="vscode.postMessage({command: 'loadTab', tab: 'tree'})">
                            点击刷新加载完整依赖树
                        </button>
                    </div>
                </div>
            </li>
        `;
    }

    html += `
            </ul>
        </div>
        <style>
            .tree-view.simple {
                font-family: var(--vscode-font-family);
                line-height: 1.6;
            }
            .summary {
                margin-bottom: 15px;
                padding: 10px;
                background-color: var(--vscode-editor-inactiveSelectionBackground);
                border-radius: 4px;
            }
            .root-list {
                list-style-type: none;
                padding-left: 0;
            }
            .root-dep {
                margin-bottom: 15px;
                padding: 10px;
                border: 1px solid var(--vscode-panel-border);
                border-radius: 4px;
            }
            .root-dep.conflict {
                border-left: 3px solid #e51400;
            }
            .dep-line {
                display: flex;
                align-items: center;
                flex-wrap: wrap;
                margin-bottom: 8px;
            }
            .dep-name {
                font-weight: bold;
                margin-right: 10px;
            }
            .dep-version {
                color: #0078d4;
                margin-right: 10px;
            }
            .dep-scope {
                color: #6c6c6c;
                font-style: italic;
            }
            .dep-children {
                margin-top: 10px;
                margin-left: 20px;
                padding-left: 10px;
                border-left: 1px dashed var(--vscode-panel-border);
            }
            .load-more {
                text-align: center;
                margin: 10px 0;
            }
            .load-more-btn {
                background-color: var(--vscode-button-background);
                color: var(--vscode-button-foreground);
                border: none;
                padding: 6px 12px;
                border-radius: 3px;
                cursor: pointer;
            }
            .load-more-btn:hover {
                background-color: var(--vscode-button-hoverBackground);
            }
            .error-message {
                color: #e51400;
                padding: 10px;
                border: 1px solid #e51400;
                border-radius: 4px;
                margin: 10px 0;
            }
            .no-dependencies {
                text-align: center;
                padding: 20px;
                color: var(--vscode-disabledForeground);
            }
        </style>
    `;

    return html;
}

// HTML转义工具函数
function escapeHtml(text: string): string {
    if (typeof text !== 'string') {
        return '';
    }
    return text
        .replace(/&/g, '&amp;')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;')
        .replace(/"/g, '&quot;')
        .replace(/'/g, '&#039;');
}
